package com.lxn.hrm.service.impl;

import com.alibaba.fastjson.JSON;
import com.lxn.dto.RegisterDTO;
import com.lxn.dto.SMSSendDTO;
import com.lxn.hrm.domain.Sso;
import com.lxn.hrm.domain.VipBase;
import com.lxn.hrm.feignclients.RedisFeignClient;
import com.lxn.hrm.mapper.SsoMapper;
import com.lxn.hrm.mapper.VipBaseMapper;
import com.lxn.hrm.service.ISsoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lxn.hrm.util.AjaxResult;
import com.lxn.hrm.util.encrypt.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author hwerth
 * @since 2020-03-03
 */
@Service
public class SsoServiceImpl extends ServiceImpl<SsoMapper, Sso> implements ISsoService {
    @Autowired
    private RedisFeignClient redisFeignClient ;
    @Autowired
    private VipBaseMapper vipBaseMapper;
    @Override
    public void register(RegisterDTO registerDTO) {
        if( !StringUtils.hasLength(registerDTO.getMobile()) ||
                !StringUtils.hasLength(registerDTO.getPassword()) ||
                !StringUtils.hasLength(registerDTO.getSmsCode())){
            throw new RuntimeException("无效参数");
        }

        //验证码判断
        String smsCodeRedisKey = "sms:"+registerDTO.getMobile();

        AjaxResult smsAjaxResult = redisFeignClient.get(smsCodeRedisKey);
        if(!smsAjaxResult.isSuccess() || smsAjaxResult.getResultObj() == null){
            throw new RuntimeException("无效验证码");
        }
        String smsCode = JSON.parseObject(smsAjaxResult.getResultObj().toString(), SMSSendDTO.class).getCode();
        if(!smsCode.equals(registerDTO.getSmsCode())){
            throw new RuntimeException("验证码错误");
        }

        //是否已经被注册
        Sso sso = baseMapper.selectByPhone(registerDTO.getMobile());
        if(sso != null){throw new RuntimeException("该手机号已经被注册");}

        long time = new Date().getTime();
        Sso registerSso = new Sso();
        registerSso.setCreateTime(time);
        registerSso.setPhone(registerDTO.getMobile());
        //MD5加密，加盐
        String salt = MD5.getRandomCode(16);

        String enPass = MD5.getMD5(registerDTO.getPassword() + salt);
        registerSso.setPassword(enPass);
        registerSso.setSalt(salt);
        baseMapper.insert(registerSso);

        //保存base表
        VipBase vipBase = new VipBase();
        vipBase.setSsoId(registerSso.getId());
        vipBase.setCreateTime(time);
        vipBase.setRegTime(time);
        vipBase.setRegChannel(1);
        vipBaseMapper.insert(vipBase);
    }

    @Override
    public String login(Sso sso) {
        if(!StringUtils.hasLength(sso.getPhone()) || !StringUtils.hasLength(sso.getPassword())){
            throw new RuntimeException("无效的登录信息");
        }

        //根据手机号查询Mysql中的sso
        Sso ssoFromMysql = baseMapper.selectByPhone(sso.getPhone());
        if(ssoFromMysql == null){
            throw new RuntimeException("无此用户");
        }

        //参数密码加密
        String salt = ssoFromMysql.getSalt();
        sso.getPassword();
        String enPassword = MD5.getMD5(sso.getPassword() + salt);
        //密码对比
        if(!ssoFromMysql.getPassword().equals(enPassword)){
            throw new RuntimeException("密码错误");
        }

        //登录成功，创建token,包含redis的key
        String token = UUID.randomUUID().toString();
        //把登录信息存储到Redis
        AjaxResult ajaxResult = redisFeignClient.setex(token, 60 * 60 , JSON.toJSONString(ssoFromMysql));
        if(!ajaxResult.isSuccess()){
            throw new RuntimeException("登录失败");
        }

        return token;
    }

}
